#!/bin/bash
# 
# desc:
#     单元测试
# author:
#     dockerlin@163.com
# 

oneTimeSetUp(){
    echo "version is ${SHUNIT_VERSION}"
    . ./index
    testResultFile=`mktemp testresultXXXXXX.txt`
    logger_info "开始单元测试"
}

oneTimeTearDown(){
    # oneTimeTearDown只执行一次
    if [ ${just_once} ]; then return 0; fi
    just_once=1
    rm ${testResultFile}
    logger_info "--测试报告--"
    #echo "用例是否成功0成功1失败 :: ${__shunit_testSuccess}"
    logger_info "测试用例总数 :: ${__shunit_testsTotal}"
    logger_info "通过用例数 :: ${__shunit_testsPassed}"
    logger_info "失败用例数 :: ${__shunit_testsFailed}"
    logger_info "总功能点 :: ${__shunit_assertsTotal}"
    logger_info "通过功能点 :: ${__shunit_assertsPassed}"
    logger_info "失败功能点 :: ${__shunit_assertsFailed}"
    logger_info "跳过功能点 :: ${__shunit_assertsSkipped}" 
    logger_info "----------"
}

testDirtool(){
    logger_info "--正在进行测试 dirtool_cxl --"
    cxlib_auto_load "${CXLIB_ROOT_PATH}/test/loadfile"
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    echoHello=`echotest`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的hello'  'hello' "${echoHello}"
    ${_ASSERT_EQUALS_} '未返回相同的返回值5'  5 ${retval}
    logger_info "--测试 dirtool_cxl 完成--"
}

testTools(){
    logger_info "--正在进行测试 tools_cxl --"
    cmdline="echo 'hello'"
    cxlib_eval_command "${cmdline}" ${testResultFile}
    result=`cat ${testResultFile}`
    ${_ASSERT_EQUALS_} '未返回相同的hello'  'hello' "${echoHello}"
    cxlib_eval_mysql "root" 'C1oudP8x&2017' 'localhost' "show databases like 'mysql%'" "${testResultFile}"
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    logger_info "--测试 tools_cxl 完成--"
}

testRequest(){
    logger_info "--正在进行测试 request_cxl --"
    result=`cxlib_sendRequest "https://127.0.0.1/Api/bssHttp" "Operate=modifymaxmember&Number=02588888888&MaxMember=33" "POST" "form" `
    logger_info "--测试 request_cxl 完成--"
}

testCrypt(){
    logger_info "--正在进行测试 crypt_cxl --"
    logger_info "--正在进行测试 cxlib_md5 --"
    md5str=`cxlib_md5 '123456'`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    ${_ASSERT_EQUALS_} '未返回相同的返回值md5值'  'e10adc3949ba59abbe56e057f20f883e' "${md5str}"
    logger_info "--正在进行测试 cxlib_sha1 --"
    md5str=`cxlib_sha1 '123456'`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    ${_ASSERT_EQUALS_} '未返回相同的返回值md5值'  '7c4a8d09ca3762af61e59520943dc26494f8941b' "${md5str}"

    logger_info "--正在进行测试 cxlib_gzip_str 和 cxlib_gunzip_str --"
    str='12345678abcdefg'
    result=`cxlib_gzip_str $str`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    result1=`cxlib_gunzip_str $result`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    ${_ASSERT_EQUALS_} '压缩和解压缩后不一样'  $str ${result1}

    logger_info "--正在进行测试 cxlib_encrypt_str 和 cxlib_decrypt_str --"
    key='226a89e66d0dcc79c9673150fa176001'
    result=`cxlib_encrypt_str $str $key`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    result1=`cxlib_decrypt_str $result $key`
    retval=$?
    ${_ASSERT_EQUALS_} '未返回相同的返回值0'  0 ${retval}
    ${_ASSERT_EQUALS_} '压缩和解压缩后不一样'  $str ${result1}

    logger_info "--测试 crypt_cxl 完成--"
}

. ./test/shunit2